package com.xb.controller;

import com.xb.entity.Article;
import com.xb.entity.User;
import com.xb.service.ArticleService;
import com.xb.utils.PageEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/article/*")
public class ArticleController extends BaseController{
    private ArticleService articleService=new ArticleService();


    /**
     * 收藏文章、取消文章
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addFavorite(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取前端所有参数
        Map<String, String> param = getParam(req);
        Long articleId=Long.parseLong(param.get("articleId"));

        //我是否收藏过该文章
        Boolean isFavorite =articleService.findFavorite(loginUser.getId(),articleId);

        if(isFavorite){
            //我是来取消文章的
            articleService.delFavorite(loginUser.getId(),articleId);
        }else {
            //我是来收藏文章的
            articleService.addFavorite(loginUser.getId(),articleId);
        }
    }

        /**
         * 查看文章的详情
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void articleDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取前端所有参数
        Map<String, String> param = getParam(req);

        Long articleId=Long.parseLong(param.get("articleId"));

        //浏览量+1
        articleService.updateBrowseCount(articleId);

        //文章详情
        Article article= articleService.findById(articleId);


        //收藏次数
        Integer favoriteCount= articleService.findFavoriteCount(articleId);

        //我是否收藏过该文章
        Boolean isFavorite =articleService.findFavorite(loginUser.getId(),articleId);

        //我关注的人，并且他们也收藏了这篇文章
        List<User> favoriteList=articleService.findUserList(loginUser.getId(),articleId);

        req.setAttribute("article",article);
        req.setAttribute("favoriteCount",favoriteCount);
        req.setAttribute("isFavorite",isFavorite);
        req.setAttribute("favoriteList",favoriteList);

        req.getRequestDispatcher("/html/article_detail.jsp").forward(req,resp);

    }


        /**
         * 我的收藏+搜索+分页
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void findFavoritePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取前端所有参数
        Map<String, String> param = getParam(req);

        // 当前页
        Integer currPage = Integer.parseInt(param.get("currPage"));

        // 搜索条件
        String title = param.get("title");

        PageEntity<Article> pageData = articleService.findFavoritePage(title, loginUser.getId(), currPage);

        req.setAttribute("pageData", pageData);
        req.setAttribute("title", title);

        req.getRequestDispatcher("/html/article_collect.jsp").forward(req, resp);


    }


        /**
         * 发布文章
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String title = req.getParameter("title");
        String content = req.getParameter("content");

        Article article= new Article(null,title,content,0L,new Date(),loginUser.getRealName(),loginUser.getId());

        articleService.save(article);
        resp.sendRedirect("/article/findPage?currPage=1");
    }


        /**
         * 文章搜索+分页
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> param = getParam(req);
        //当前页的数据
        Integer currPage = Integer.parseInt(param.get("currPage"));
        String title=param.get("title");

        PageEntity<Article> pageData=articleService.findPage(title,currPage);
        req.setAttribute("pageData",pageData);
        req.setAttribute("title",title);

        req.getRequestDispatcher("/html/article.jsp").forward(req,resp);
    }



    }
